﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;

namespace Cms.Application.Admin.Presenters
{
    public class EditRssFeedPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IEditRssFeedView _view;

        public EditRssFeedPresenter(IEditRssFeedView view)
            : this(view, new RepositoryFactory())
        {
        }

        public EditRssFeedPresenter(IEditRssFeedView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditRssFeed)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            IList<Category> categories;
            _view.SetFolders(_administrator.CurrentSite.Domain, _administrator.CurrentSite.GetRootFolders());
            _view.SetPages(_administrator.CurrentSite.GetRootPages());
            if (_view.RssFeedID == -1)
            {
                categories = new List<Category>();
                _view.SelectedContentDisplayMode = RssFeedContentDisplayMode.Complete;
                UpdatePages();
            }
            else
            {
                RssFeed rssFeed = ContentRepository.GetRssFeed(_view.RssFeedID);
                _view.Name = rssFeed.Name;
                _view.Filename = rssFeed.Filename.Substring(0, rssFeed.Filename.Length - 5); // Remove ".aspx"
                _view.Description = rssFeed.Description;
                categories = rssFeed.Categories;
                if (rssFeed.ContentDisplayPage.Folder == null)
                {
                    _view.SelectedFolderID = -1;
                    _view.SortMode = ContentSortMode.PublishedTimestamp;
                }
                else
                {
                    _view.SelectedFolderID = rssFeed.ContentDisplayPage.Folder.ID;
                    _view.SetPages(rssFeed.ContentDisplayPage.Folder.Pages);
                }
                _view.SelectedPageID = rssFeed.ContentDisplayPage.ID;
                _view.SelectedContentDisplayMode = rssFeed.ContentDisplayMode;
                _view.RemoveImgTags = rssFeed.RemoveImgTags;
                _view.SortMode = rssFeed.SortMode;
            }
            // Set up categories
            _view.SetCategories(_administrator.CurrentSite.GetRootCategories(), new List<Category>(), categories);
        }

        public void UpdatePages()
        {
            _view.SetPages(_view.SelectedFolderID == -1
                               ? _administrator.CurrentSite.GetRootPages()
                               : FolderRepository.GetFolder(_view.SelectedFolderID).Pages);
        }

        public void Save()
        {
            if (_view.Name.Trim() == string.Empty)
            {
                AddValidationError(1, GetText(_view, "NoNameError"));
            }
            if (_view.Description.Trim() == string.Empty)
            {
                AddValidationError(1, GetText(_view, "NoDescriptionError"));
            }
            if (_view.Filename.Trim() == string.Empty)
            {
                AddValidationError(2, GetText(_view, "NoFilenameError"));
            }
            else if (!RegexValidation.NameIsFileSystemCompliant(_view.Filename.Trim()))
            {
                AddValidationError(4, GetText(_view, "NameNotFileSystemCompliantError"));
            }
            string filename = _view.Filename;
            if (!filename.ToLower().EndsWith(".aspx"))
            {
                filename += ".aspx";
            }
            if (new List<Category>(_view.GetSelectedCategories()).Count == 0)
            {
                AddValidationError(5, GetText(_view, "NoCategoriesError"));
            }
            if (!IsValid)
                return;
            RssFeed rssFeed;
            if (_view.RssFeedID == -1)
            {
                rssFeed = new RssFeed { Site = _administrator.CurrentSite };
                rssFeed.Site.RssFeeds.Add(rssFeed);
            }
            else
            {
                rssFeed = ContentRepository.GetRssFeed(_view.RssFeedID);
            }
            rssFeed.Name = _view.Name;
            rssFeed.Filename = filename;
            rssFeed.Description = _view.Description;
            rssFeed.ContentDisplayPage = PageRepository.GetPage(_view.SelectedPageID);
            rssFeed.ContentDisplayMode = _view.SelectedContentDisplayMode;
            rssFeed.RemoveImgTags = _view.RemoveImgTags;
            rssFeed.SortMode = _view.SortMode;

            // Clear and collect categories)
            foreach (Category category in rssFeed.Categories)
                category.RssFeeds.Remove(rssFeed);
            rssFeed.Categories.Clear();
            foreach (Category category in _view.GetSelectedCategories())
            {
                rssFeed.Categories.Add(category);
                category.RssFeeds.Add(rssFeed);
            }
            SiteRepository.SaveSite(_administrator.CurrentSite);
            _view.SetRssFeedID(rssFeed.ID);
        }
    }
}